﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using FU.Capstones.IMS.Services.Common;
using FU.Capstones.IMS.Services.Common.DataContracts;
using FU.Capstones.IMS.Services.Common.Respondents;
using FU.Capstones.IMS.Services.Interfaces;

namespace FU.Capstones.IMS.Services.DAOs
{
    public class WarehouseDAO : DAOBase, IWarehouseDAO
    {
        #region IWarehouseDAO Members

        public CollectionFeedback<Warehouse> GetCollection(bool isShowAll)
        {
            var feedback = new CollectionFeedback<Warehouse> {IsSuccessful = false, Data = new List<Warehouse>()};
            try
            {
                OpenConnection();

                var command = new SqlCommand("GetCollectionWarehouse", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@IsShowAll", isShowAll);

                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    var warehouse = new Warehouse();

                    warehouse.WarehouseID = Convert.ToInt32(reader["WarehouseID"]);
                    warehouse.WarehouseName = Convert.ToString(reader["WarehouseName"]);
                    warehouse.Address = Convert.ToString(reader["Address"]);
                    warehouse.OfficePhone = Convert.ToString(reader["OfficePhone"]);
                    warehouse.Description = reader["Description"].ToString();
                    warehouse.IsDelete = Convert.ToBoolean(reader["IsDelete"]);

                    feedback.Data.Add(warehouse);
                }

                feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                feedback.IsSuccessful = true;


                //Log.Info("GetWarehouseCollection: {0}", DateTime.Now);
            }
            catch (SqlException se)
            {
                //Log.Error("GetWarehouseCollection - SqlException: {0} - {1}", se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                //Log.Error("GetWarehouseCollection - Exception: {0} - {1}", e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public CollectionFeedback<Warehouse> GetCollectionType(bool isShowAll)
        {
            var feedback = new CollectionFeedback<Warehouse> {IsSuccessful = false, Data = new List<Warehouse>()};
            try
            {
                OpenConnection();

                var command = new SqlCommand("GetCollectionWarehouse", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@IsShowAll", isShowAll);

                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    var warehouse = new Warehouse();

                    warehouse.WarehouseID = Convert.ToInt32(reader["WarehouseID"]);
                    warehouse.WarehouseName = Convert.ToString(reader["WarehouseName"]);
                    warehouse.Address = Convert.ToString(reader["Address"]);
                    warehouse.OfficePhone = Convert.ToString(reader["OfficePhone"]);
                    warehouse.Description = reader["Description"].ToString();
                    warehouse.IsDelete = Convert.ToBoolean(reader["IsDelete"]);

                    feedback.Data.Add(warehouse);
                }

                feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                feedback.IsSuccessful = true;


                //Log.Info("GetWarehouseCollection: {0}", DateTime.Now);
            }
            catch (SqlException se)
            {
                //Log.Error("GetWarehouseCollection - SqlException: {0} - {1}", se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                //Log.Error("GetWarehouseCollection - Exception: {0} - {1}", e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public SingleFeedback<Warehouse> GetSingle(int objectID)
        {
            var feedback = new SingleFeedback<Warehouse>
                               {
                                   IsSuccessful = false,
                                   Data =
                                       new Warehouse
                                           {
                                               MaterialCollection = new List<Material>(),
                                               ProductCollection = new List<Product>(),
                                               Manager = new Employee()
                                           }
                               };
            try
            {
                var command = new SqlCommand("GetSingleWarehouse", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@WarehouseID", objectID);

                OpenConnection();

                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    feedback.Data.WarehouseID = Convert.ToInt32(reader["WarehouseID"]);
                    feedback.Data.WarehouseName = Convert.ToString(reader["WarehouseName"]);
                    feedback.Data.Address = Convert.ToString(reader["Address"]);
                    feedback.Data.OfficePhone = Convert.ToString(reader["OfficePhone"]);
                    feedback.Data.Description = reader["Description"].ToString();
                    feedback.Data.Manager.EmployeeID = Convert.ToInt32(reader["Manager"]);
                    feedback.Data.Manager.FullName = Convert.ToString(reader["FullName"]);
                    feedback.Data.IsDelete = Convert.ToBoolean(reader["IsDelete"]);

                    break;
                }

                if (reader.NextResult())
                {
                    while (reader.Read())
                    {
                        Material material = new Material();
                        material.MaterialID = Convert.ToInt32(reader["MaterialID"]);
                        material.MaterialCode = Convert.ToString(reader["MaterialCode"]);
                        material.MaterialName = Convert.ToString(reader["MaterialName"]);
                        material.Description = Convert.ToString(reader["Description"]);
                        material.Quantity = Convert.ToInt32(reader["Quantity"]);

                        feedback.Data.MaterialCollection.Add(material);
                    }
                }

                if (reader.NextResult())
                {
                    while (reader.Read())
                    {
                        Product product = new Product();
                        product.ProductID = Convert.ToInt32(reader["ProductID"]);
                        product.ProductCode = Convert.ToString(reader["ProductCode"]);
                        product.ProductName = Convert.ToString(reader["ProductName"]);
                        product.Description = Convert.ToString(reader["Description"]);
                        product.Quantity = Convert.ToInt32(reader["Quantity"]);

                        feedback.Data.ProductCollection.Add(product);
                    }
                }


                feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                feedback.IsSuccessful = true;

                Log.Info("GetSingleWarehouse: {0} - {1}", objectID, DateTime.Now);
            }
            catch (SqlException se)
            {
                //Log.Error("GetSingleWarehouse - SqlException: {0} - {1}", se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                //Log.Error("GetSingleWarehouse - Exception: {0} - {1}", e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public SingleFeedback<int> Insert(Warehouse data)
        {
            var feedback = new SingleFeedback<int> {IsSuccessful = false};
            try
            {
                var command = new SqlCommand("InsertWarehouse", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@WarehouseName", data.WarehouseName);
                command.Parameters.AddWithValue("@Description", data.OfficePhone);
                command.Parameters.AddWithValue("@WarehouseName", data.Address);
                command.Parameters.AddWithValue("@Description", data.Description);
                command.Parameters.AddWithValue("@Manager", data.Manager.EmployeeID);

                OpenConnection();

                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        int warehouseID = Convert.ToInt32(reader["WarehouseID"]);

                        feedback.Data = warehouseID;
                        feedback.Message = InventoryConstants.Messages.InsertSuccessfully;
                        feedback.IsSuccessful = true;

                        break;
                    }
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.InsertFailed;
                    feedback.IsSuccessful = false;
                    feedback.Data = -1;
                }

                //Log.Info("InsertWarehouse: {0} - {1}", feedback.Data, DateTime.Now);
            }
            catch (SqlException se)
            {
                //Log.Error("InsertWarehouse - SqlException: {0} - {1}", se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                //Log.Error("InsertWarehouse - Exception: {0} - {1}", e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public Feedback Update(Warehouse data)
        {
            var feedback = new Feedback {IsSuccessful = false};
            try
            {
                var command = new SqlCommand("UpdateWarehouse", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@WarehouseID", data.WarehouseID);
                command.Parameters.AddWithValue("@WarehouseName", data.WarehouseName);
                command.Parameters.AddWithValue("@OfficePhone", data.OfficePhone);
                command.Parameters.AddWithValue("@Address", data.Address);
                command.Parameters.AddWithValue("@Description", data.Description);
                command.Parameters.AddWithValue("@Manager", data.Manager.EmployeeID);

                OpenConnection();

                int rowCount = command.ExecuteNonQuery();
                if (rowCount == 1)
                {
                    feedback.Message = InventoryConstants.Messages.UpdateSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.UpdateFailed;
                    feedback.IsSuccessful = false;
                }

               //Log.Info("UpdateWarehouse: {0} - {1} - {2}", data.WarehouseID, feedback.Message, DateTime.Now);
            }
            catch (SqlException se)
            {
                Log.Error("UpdateWarehouse - SqlException: {0} - {1} - {2}", data.WarehouseID, se.StackTrace,
                          DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                //Log.Error("UpdateWarehouse - Exception: {0} - {1} - {2}", data.WarehouseID, e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public Feedback Delete(int objID)
        {
            var feedback = new Feedback {IsSuccessful = false};
            try
            {
                var command = new SqlCommand("DeleteWarehouse", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@WarehouseID", objID);

                OpenConnection();

                int rowCount = command.ExecuteNonQuery();
                if (rowCount == 1)
                {
                    feedback.Message = InventoryConstants.Messages.DeleteSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.DeleteFailed;
                    feedback.IsSuccessful = false;
                }

                //Log.Info("DeleteWarehouse: {0} - {1} - {2}", objID, feedback.Message, DateTime.Now);
            }
            catch (SqlException se)
            {
                //Log.Error("DeleteWarehouse - SqlException: {0} - {1} - {2}", objID, se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                //Log.Error("DeleteWarehouse - Exception: {0} - {1} - {2}", objID, e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public CollectionFeedback<Warehouse> Search(Warehouse criteria)
        {
            throw new NotImplementedException();
        }

        public Feedback UndoDelete(int objID)
        {
            var feedback = new Feedback {IsSuccessful = false};
            try
            {
                var command = new SqlCommand("UndoDeletedWarehouse", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@WarehouseID", objID);

                OpenConnection();

                int rowCount = command.ExecuteNonQuery();
                if (rowCount == 1)
                {
                    feedback.Message = InventoryConstants.Messages.UndoDeleteSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.UndoDeleteFailed;
                    feedback.IsSuccessful = false;
                }

                //Log.Info("UndoDeletedWarehouse: {0} - {1} - {2}", objID, feedback.Message, DateTime.Now);
            }
            catch (SqlException se)
            {
                //Log.Error("UndoDeletedWarehouse - SqlException: {0} - {1} - {2}", objID, se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                //Log.Error("UndoDeletedWarehouse - Exception: {0} - {1} - {2}", objID, e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        #endregion
    }
}
